import numpy as np
from finhack.market.astock.astock import AStock


#财务指标，anndate需要加1，防止未来函数
def financeIndicator(df,p):
            df_fi=AStock.alignStockFactors(df,'astock_finance_indicator','ann_date',filed='*',conv=3,db='tushare') 
            df=df.reset_index(drop=0)
            df_fi=df_fi.reset_index(drop=0)
            
            if df_fi.empty:
                return df_fi
            
            if(len(df_fi)!=len(df)):
                print('len(df_fi)!=len(df)!')
            
 
            df['eps']=df_fi['eps']
            df['dtEps']=df_fi['dt_eps']
            df['totalRevenuePs']=df_fi['total_revenue_ps']
            df['revenuePs']=df_fi['revenue_ps']
            df['capitalResePs']=df_fi['capital_rese_ps']
            df['surplusResePs']=df_fi['surplus_rese_ps']
            df['undistProfitPs']=df_fi['undist_profit_ps']
            df['extraItem']=df_fi['extra_item']
            df['profitDedt']=df_fi['profit_dedt']
            df['grossMargin']=df_fi['gross_margin']
            df['currentRatio']=df_fi['current_ratio']
            df['quickRatio']=df_fi['quick_ratio']
            df['cashRatio']=df_fi['cash_ratio']
            df['arTurn']=df_fi['ar_turn']
            df['caTurn']=df_fi['ca_turn']
            df['faTurn']=df_fi['fa_turn']
            df['assetsTurn']=df_fi['assets_turn']
            df['opIncome']=df_fi['op_income']
            df['ebit']=df_fi['ebit']
            df['ebitda']=df_fi['ebitda']
            df['fcff']=df_fi['fcff']
            df['fcfe']=df_fi['fcfe']
            df['currentExint']=df_fi['current_exint']
            df['noncurrentExint']=df_fi['noncurrent_exint']
            df['interestdebt']=df_fi['interestdebt']
            df['netdebt']=df_fi['netdebt']
            df['tangibleAsset']=df_fi['tangible_asset']
            df['workingCapital']=df_fi['working_capital']
            df['networkingCapital']=df_fi['networking_capital']
            df['investCapital']=df_fi['invest_capital']
            df['retainedEarnings']=df_fi['retained_earnings']
            df['diluted2Eps']=df_fi['diluted2_eps']
            df['bps']=df_fi['bps']
            df['ocfps']=df_fi['ocfps']
            df['retainedps']=df_fi['retainedps']
            df['cfps']=df_fi['cfps']
            df['ebitPs']=df_fi['ebit_ps']
            df['fcffPs']=df_fi['fcff_ps']
            df['fcfePs']=df_fi['fcfe_ps']
            df['netprofitMargin']=df_fi['netprofit_margin']
            df['grossprofitMargin']=df_fi['grossprofit_margin']
            df['cogsOfSales']=df_fi['cogs_of_sales']
            df['expenseOfSales']=df_fi['expense_of_sales']
            df['profitToGr']=df_fi['profit_to_gr']
            df['saleexpToGr']=df_fi['saleexp_to_gr']
            df['adminexpOfGr']=df_fi['adminexp_of_gr']
            df['finaexpOfGr']=df_fi['finaexp_of_gr']
            df['impaiTtm']=df_fi['impai_ttm']
            df['gcOfGr']=df_fi['gc_of_gr']
            df['opOfGr']=df_fi['op_of_gr']
            df['ebitOfGr']=df_fi['ebit_of_gr']
            df['roe']=df_fi['roe']
            df['roeWaa']=df_fi['roe_waa']
            df['roeDt']=df_fi['roe_dt']
            df['roa']=df_fi['roa']
            df['npta']=df_fi['npta']
            df['roic']=df_fi['roic']
            df['roeYearly']=df_fi['roe_yearly']
            df['roa2Yearly']=df_fi['roa2_yearly']
            df['debtToAssets']=df_fi['debt_to_assets']
            df['assetsToEqt']=df_fi['assets_to_eqt']
            df['dpAssetsToEqt']=df_fi['dp_assets_to_eqt']
            df['caToAssets']=df_fi['ca_to_assets']
            df['ncaToAssets']=df_fi['nca_to_assets']
            df['tbassetsToTotalassets']=df_fi['tbassets_to_totalassets']
            df['intToTalcap']=df_fi['int_to_talcap']
            df['eqtToTalcapital']=df_fi['eqt_to_talcapital']
            df['currentdebtToDebt']=df_fi['currentdebt_to_debt']
            df['longdebToDebt']=df_fi['longdeb_to_debt']
            df['ocfToShortdebt']=df_fi['ocf_to_shortdebt']
            df['debtToEqt']=df_fi['debt_to_eqt']
            df['eqtToDebt']=df_fi['eqt_to_debt']
            df['eqtToInterestdebt']=df_fi['eqt_to_interestdebt']
            df['tangibleassetToDebt']=df_fi['tangibleasset_to_debt']
            df['tangassetToIntdebt']=df_fi['tangasset_to_intdebt']
            df['tangibleassetToNetdebt']=df_fi['tangibleasset_to_netdebt']
            df['ocfToDebt']=df_fi['ocf_to_debt']
            df['turnDays']=df_fi['turn_days']
            df['roaYearly']=df_fi['roa_yearly']
            df['roaDp']=df_fi['roa_dp']
            df['fixedAssets']=df_fi['fixed_assets']
            df['profitToOp']=df_fi['profit_to_op']
            df['qSaleexpToGr']=df_fi['q_saleexp_to_gr']
            df['qGcToGr']=df_fi['q_gc_to_gr']
            df['qRoe']=df_fi['q_roe']
            df['qDtRoe']=df_fi['q_dt_roe']
            df['qNpta']=df_fi['q_npta']
            df['qOcfToSales']=df_fi['q_ocf_to_sales']
            df['basicEpsYoy']=df_fi['basic_eps_yoy']
            df['dtEpsYoy']=df_fi['dt_eps_yoy']
            df['cfpsYoy']=df_fi['cfps_yoy']
            df['opYoy']=df_fi['op_yoy']
            df['ebtYoy']=df_fi['ebt_yoy']
            df['netprofitYoy']=df_fi['netprofit_yoy']
            df['dtNetprofitYoy']=df_fi['dt_netprofit_yoy']
            df['ocfYoy']=df_fi['ocf_yoy']
            df['roeYoy']=df_fi['roe_yoy']
            df['bpsYoy']=df_fi['bps_yoy']
            df['assetsYoy']=df_fi['assets_yoy']
            df['eqtYoy']=df_fi['eqt_yoy']
            df['trYoy']=df_fi['tr_yoy']
            df['orYoy']=df_fi['or_yoy']
            df['qSalesYoy']=df_fi['q_sales_yoy']
            df['qOpQoq']=df_fi['q_op_qoq']
            df['equityYoy']=df_fi['equity_yoy']
            del df_fi
            
            # print(df)
            # print('--------------')
            return df

